package com.goods.business.service.imp;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.business.mapper.SupplierMapper;
import com.goods.business.service.SupplierService;
import com.goods.common.model.business.Supplier;
import com.goods.common.vo.business.SupplierVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

/**
 * @author ：Hachimen
 * @date ：Created in 2021/9/24 15:33
 * @description：
 * @modified By：
 * @version:
 */
@Service
public class SupplierServiceImpl implements SupplierService {
    @Autowired
    private SupplierMapper supplierMapper;

    @Override
    public PageInfo<Supplier> findSupplierList(String pageNum, String pageSize, String name, String contact,String address) {
        if(!StringUtils.isEmpty(pageNum)&&!StringUtils.isEmpty(pageSize)){
            PageHelper.startPage(Integer.parseInt(pageNum),Integer.parseInt(pageSize));
        }

        Example example = new Example(Supplier.class);
        Example.Criteria criteria = example.createCriteria();

        if(!StringUtils.isEmpty(name)){
            criteria.andLike("name","%"+name+"%");
        }
        if(!StringUtils.isEmpty(contact)){
            criteria.andLike("contact","%"+contact+"%");
        }
        if(!StringUtils.isEmpty(address)){
            criteria.andLike("address","%"+address+"%");
        }

        List<Supplier> suppliers = supplierMapper.selectByExample(example);
        return new PageInfo<Supplier>(suppliers);
    }

    @Override
    public Supplier add(SupplierVO supplierVo) {
        Supplier supplier = new Supplier();
        BeanUtils.copyProperties(supplierVo,supplier);
        supplier.setCreateTime(new Date());
        supplier.setModifiedTime(new Date());
        supplierMapper.insert(supplier);
        return supplier;
    }

    @Override
    public SupplierVO edit(Long id) {
        Supplier supplier = supplierMapper.selectByPrimaryKey(id);
        SupplierVO supplierVO = new SupplierVO();
        BeanUtils.copyProperties(supplier,supplierVO);
        return supplierVO;
    }

    @Override
    public void update(SupplierVO supplierVo) {
        Supplier supplier = new Supplier();
        BeanUtils.copyProperties(supplierVo,supplier);
        supplier.setModifiedTime(new Date());
        supplierMapper.updateByPrimaryKey(supplier);
    }

    @Override
    public void delete(Long id) {
        supplierMapper.deleteByPrimaryKey(id);
    }
}
